
#include "../DataBase_h/DataBaseManager.h"
#include "../DataBase_h/DataBaseConfig.h"
#include "../DataBase_h/DataTableConfig.h"
#include "../Error_h/error.h"

#include <QtSql/QSqlQuery>


int DataBaseManager::Connect() {

    _db = QSqlDatabase::addDatabase("QMYSQL");
    _db.setHostName(DataBaseConfig::GetInstance()->GetIp());              //设置你远程服务器的IP
    _db.setDatabaseName(DataBaseConfig::GetInstance()->GetDataBaseName());//数据库名
    _db.setUserName(DataBaseConfig::GetInstance()->GetUserName());        //数据库用户名和密码
    _db.setPassword(DataBaseConfig::GetInstance()->GetUserPwd());

    if (!_db.open()){
        return DATABASE_CONNECT_ERROR;
    }
    else{
        return NONE;
    }

}

void DataBaseManager::DisConnect(){
    _db.close();
}

int DataBaseManager::CheckUser(QString userId, QString userName, UserTable userTable){
    QString sql = "select * from "+ userTable.tableName+
                    "where userId = "+userTable.userId +"and userPwd = "+userTable.userPwd;
    QSqlQuery query;
    query.exec(sql);
    if(query.next()) return NONE;
    else return SELECT_ERROR;

}

